import pygame
import random
from pygame.locals import *
from random import randrange
from settings import Settings

my_set = Settings()
obstacle_images = ['../graphics/obstacles/obstacle1.png','../graphics/obstacles/obstacle2.png','../graphics/obstacles/obstacle3.png',]
object_name = {0:'Stomach acid regurgitation', 1:'Food debris_1', 2:'Food debris_2', 3:'mucus',}
# 定义一个场景类
class ObjectClass(pygame.sprite.Sprite):

    def __init__(self,speed,objectFlag):  #传入两个参数，初始速度和bool值障碍物或者金币标记
        pygame.sprite.Sprite.__init__(self)  #父类的初始化方法要调用
        '''
        :param image =图片
        :param type 判断是障碍物还是金币
        :param speed 速度
        '''
        if objectFlag:
            self.obstaclenum = random.randint(0,2)
            self.image = pygame.image.load(obstacle_images[self.obstaclenum])
            self.type = 'obstacle'
        else:
            self.image = pygame.image.load('../graphics/coin.png')
            self.type = 'coin'
            self.obstaclenum = 3
        self.name = object_name[self.obstaclenum]
        self.rect = self.image.get_rect()
        self.rect.top = my_set.screen_height - self.rect.height
        self.rect.centerx = randrange(my_set.screen_width - self.rect.width) + self.rect.width / 2
        self.speed = speed

    def update(self, *args):
        self.rect.top -= self.speed
        if self.rect.top < -self.rect.height:
            self.kill()

class Destination(pygame.sprite.Sprite):
    def __init__(self):
        self.frame_width = 0
        self.frame_height = 0
        self.rect = 0,0,0,0
        self.columns = 0
        self.last_frame = 1
        self.last_time = 0
        self.frame = 0
        self.first_frame = 0
        self.old_frame = 0

    def load(self, filename, row, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()   # 载入整张图片
        self.master_rect = self.master_image.get_rect()                   # 获取图片的rect值
        self.frame_width = self.master_rect.width//columns                # 计算单一帧的宽度
        self.frame_height = self.master_rect.height//row                  # 计算单一帧的高度
        self.rect = (my_set.screen_width-self.frame_width)/2, my_set.screen_height-self.frame_height, self.frame_width, self.frame_height             # 更新rect
        self.columns = columns                                            # 存储列的值（用以后续计算）
        self.last_frame = row * columns - 1                               # 计算是有0开始的，需要 -1

    def update(self, screen):
        self.frame += 1                                                   # 帧序号 +1
        if self.frame > self.last_frame:
            self.frame = self.first_frame                                 # 循环播放
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width           # 计算 subsurface 的 x 坐标
            frame_y = (self.frame // self.columns) * self.frame_height         # 计算 subsurface 的 y 坐标
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height) # 获取subsurface 的 rect
            self.image = self.master_image.subsurface(rect)                    # 更新self.image
            self.old_frame = self.frame                                        # 更新self.old_frame
        screen.blit(self.image, self.rect)   